package com.ljkj.cordial.chat.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.ChatAdapter;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.ChatTableUnit;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.bean.PayLoad;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.nic.view.widget.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 阅后即焚
 */
public class ReadFinishTextPresenter extends AChatPresenter {
    public ReadFinishTextPresenter(int direction) {
        super(direction);
//        LayoutInflater.from(view.getContext()).inflate(R.layout.item_chat_right_read_finish, null);
    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(View view) {
        if (getDirection() == LEFT) {
            return new ViewLeftHolder(view);
        }
        return new ViewRightHolder(view);
    }

    @Override
    public void bindViewHolder(RecyclerView.ViewHolder holder, Object obj, int position) {
        ChatTableUnit item = (ChatTableUnit) obj;
        if (getDirection() == LEFT) {
            ViewLeftHolder viewLeftHolder = (ViewLeftHolder) holder;
//            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewLeftHolder.tvHistoryHint.getLayoutParams();
//            Context context = viewLeftHolder.tvHistoryHint.getContext();
//            if (getChatType() == Canstants.SINGLE_CHAT) {
//                viewLeftHolder.tvTitle.setVisibility(View.GONE);
//                params.topMargin = context.getResources().getDimensionPixelOffset(R.dimen.icon_friend_title_size);
//                viewLeftHolder.tvHistoryHint.setLayoutParams(params);
//            } else {
//                viewLeftHolder.tvTitle.setText(item.getTargetName());
//            }
//            setVisible(viewLeftHolder.cb, item.isVisible());
//            setCheck(viewLeftHolder.cb, item.isChecked());
//            viewLeftHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
////                    item.setChecked(isChecked);
////                    if (getOnCListener() != null) {
////                        getOnCListener().onItemCheckChange(buttonView, item, position, isChecked);
////                    }
//                }
//            });
//            viewLeftHolder.tvHistoryHint.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
////                    if (getOnCListener() != null) {
////                        getOnCListener().onItemLongClick(v, item, position);
////                        return true;
////                    }
//                    return false;
//                }
//            });
//            if (isLoadAccess()) {
//                loadAvatar(item, viewLeftHolder.ivAvatar);
//            }
//            AlbumManageUtil.setImageColorMatrix(viewLeftHolder.ivAvatar, 0f);
//            AlbumManageUtil.setImageColorMatrix(viewLeftHolder.ivIsVisitor, 0f);
            if (item.getChatType() == MsgType.Del_Chat_Msg) {
//                viewLeftHolder.tvHistoryHint.setText("此消息已被撤回");
                viewLeftHolder.view.setVisibility(View.VISIBLE);
            } else {
                viewLeftHolder.view.setVisibility(View.GONE);
            }
            viewLeftHolder.ivIsVisitor.setVisibility(IMSContacts.UserType.TOURISTS_USER.equals(item.getTargetType()) ? View.VISIBLE : View.GONE);
        } else {
            ViewRightHolder viewRightHolder = (ViewRightHolder) holder;
//            setVisible(viewRightHolder.cb, item.isVisible());
//            if (item.isVisible()) {
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRightHolder.ivAvatar.getLayoutParams();
//                params.addRule(RelativeLayout.LEFT_OF, viewRightHolder.cb.getId());
//                params.rightMargin = viewRightHolder.cb.getContext().getResources().getDimensionPixelOffset(R.dimen.dimen_48);
//            } else {
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRightHolder.ivAvatar.getLayoutParams();
//                params.rightMargin = viewRightHolder.cb.getContext().getResources().getDimensionPixelOffset(R.dimen.dimen_16);
//            }
//            setCheck(viewRightHolder.cb, item.isChecked());
//            viewRightHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
////                    item.setChecked(isChecked);
////                    if (getOnCListener() != null) {
////                        getOnCListener().onItemCheckChange(buttonView, item, position, isChecked);
////                    }
//                }
//            });
//            viewRightHolder.tvHistoryHint.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
////                    if (getOnCListener() != null) {
////                        getOnCListener().onItemLongClick(v, item, position);
////                        return true;
////                    }
//                    return false;
//                }
//            });
//            if (isLoadAccess()) {
//                loadAvatar(item, viewRightHolder.ivAvatar);
//            }
            if (item.getChatType() == MsgType.Del_Chat_Msg) {
                viewRightHolder.view.setVisibility(View.VISIBLE);
            } else {
                viewRightHolder.view.setVisibility(View.GONE);
            }
//            AlbumManageUtil.setImageColorMatrix(viewRightHolder.ivAvatar, 0f);
        }

    }

    @Override
    public void bindViewHolder(ChatAdapter chatAdapter, int position, RecyclerView.ViewHolder holder, Object obj, PayLoad payLoad) {
        super.bindViewHolder(chatAdapter, position, holder, obj, payLoad);
        ChatTableUnit item = (ChatTableUnit) obj;
        if (getDirection() == LEFT) {
            ViewLeftHolder viewLeftHolder = (ViewLeftHolder) holder;
//            for (int type : payLoad.getType()) {
//                if (PayLoad.AVATAR == type)
//                    loadAvatar(item, viewLeftHolder.ivAvatar);
//            }
            if (item.getChatType() == MsgType.Del_Chat_Msg) {
                viewLeftHolder.view.setVisibility(View.VISIBLE);
            } else {
                viewLeftHolder.view.setVisibility(View.GONE);
            }
        } else {
            ViewRightHolder viewRightHolder = (ViewRightHolder) holder;
//            for (int type : payLoad.getType()) {
//                if (PayLoad.AVATAR == type)
//                    loadAvatar(item, viewRightHolder.ivAvatar);
//            }
            if (item.getChatType() == MsgType.Del_Chat_Msg) {
                viewRightHolder.view.setVisibility(View.VISIBLE);
            } else {
                viewRightHolder.view.setVisibility(View.GONE);
            }
        }
    }

    public static class ViewLeftHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cb)
        CheckBox cb;
        @BindView(R.id.iv_avatar)
        CircleImageView ivAvatar;
        @BindView(R.id.iv_is_visitor)
        ImageView ivIsVisitor;
        @BindView(R.id.ll_avatar)
        FrameLayout llAvatar;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_history_hint)
        TextView tvHistoryHint;
        @BindView(R.id.view)
        View view;

        ViewLeftHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public static class ViewRightHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cb)
        CheckBox cb;
        @BindView(R.id.iv_avatar)
        CircleImageView ivAvatar;
        @BindView(R.id.tv_history_hint)
        TextView tvHistoryHint;
        @BindView(R.id.view)
        View view;

        ViewRightHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
